const {
  app,
  BrowserWindow,
  Menu,
  Tray,
  dialog
} = require("electron");

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    icon: __dirname + "/ico/ico2.png",
    minWidth: 900,
    minHeight: 600
    // width: 900,
    // height: 600
    //   resizable: false
  })
  // mainWindow.maximize();

  // and load the index.html of the app.
  mainWindow.loadURL('file://' + __dirname + '/index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  mainWindow.on("close", function (event) {
    if (mainWindow.isVisible()) {
      event.preventDefault();
      mainWindow.hide();
    }
  })
  // mainWindow.on('closed', function() {
  //     mainWindow = null;
  // });
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
});

app.on("activate", function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
});

const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
  // Someone tried to run a second instance, we should focus our window.
  if (mainWindow) {
    if (mainWindow.isMinimized()) mainWindow.restore();
    if (!mainWindow.isVisible()) mainWindow.show();
    mainWindow.focus();
  }
});
if (shouldQuit) {
  app.quit();
}

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

var appTray = null
app.on("ready", function () {
  appTray = new Tray(__dirname + "/ico/ico2.png")
  var trayMenu = Menu.buildFromTemplate([{
    label: "隐藏/显示",
    type: "normal",
    click: function () {
      if (mainWindow.isVisible()) mainWindow.hide();
      else mainWindow.show();
    }
  }, {
    label: "退出",
    type: "normal",
    click: function () {
      app.exit(0);
    }
  }])
  appTray.setToolTip('This is my application.')
  appTray.setContextMenu(trayMenu)
});

var menubarTemplate = [{
  label: "窗口",
  submenu: [{
    label: "重新加载",
    accelerator: "CmdOrCtrl + R",
    click: function (item, focusedWindow) {
      if (focusedWindow) focusedWindow.reload();
    }
  }, {
    label: "打开开发人员工具",
    accelerator: (function () {
      if (process.platform === 'darwin') {
        return 'Alt+Command+I'
      } else {
        return 'Ctrl+Shift+I'
      }
    })(),
    click: function (item, focusedWindow) {
      if (focusedWindow) focusedWindow.toggleDevTools()
    }
  }, {
    label: "退出",
    accelerator: "CmdOrCtrl + w",
    // role: "close"
    click: function () {
      app.exit(0);
    }
  }]
}, {
  label: "帮助",
  submenu: [{
    label: "关于",
    click: function () {
      var msg = app.getName() + "\n" +
        "a web-client of stock on ubuntu, develope by Electron\n" +
        app.getVersion();
      dialog.showMessageBox(mainWindow, {
        type: "info",
        title: "关于",
        message: msg,
        buttons: ["OK"]
      });
    }
  }]
}];
var menubar = Menu.buildFromTemplate(menubarTemplate)
Menu.setApplicationMenu(menubar)
